<?php

namespace addons\yunfood\model;

use addons\yunfood\library\ModelTrait;
use app\common\model\User;
use think\Model;

/**
 * 用户授权
 */
class UserAuth extends Model
{
    use ModelTrait;

    // 表名,不含前缀
    protected $name = 'yunfood_user_auth';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $updateTime = 'updated_at';
    protected $createTime = 'created_at';

    protected $resultSetType = 'collection';

    public function scopeShow($query)
    {
        return $query->where('status', '=', self::STATUS_NORMAL);
    }

    // 用户数据
    public function userData()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }

    // 来源
    const SOURCE_MINI = 'mini';
    const SOURCE_MP   = 'mp';

    public static function getSourceList()
    {
        return [self::SOURCE_MINI => __('微信小程序'), self::SOURCE_MP => __('微信公众号')];
    }

    public function getSourceTextAttr($value, $data)
    {
        $optionList = self::getSourceList();

        return $optionList[$data['source']] ?? '';
    }

    // 状态
    const STATUS_NORMAL = 1;
    const STATUS_HIDDEN = 2;

    public static function getStatusList()
    {
        return [self::STATUS_NORMAL => __('Normal'), self::STATUS_HIDDEN => __('屏蔽')];
    }

    public function getStatusTextAttr($value, $data)
    {
        $statusList = self::getStatusList();

        return $statusList[$data['status']] ?? $data['status'];
    }
}
